# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Name:         sharpmis.py
# Purpose:      The sharpmis application
#
# Author:       Zhang Kun
#
# Created:      1/21/08
# Copyright:    (c) 2007-2008 HanMingSoft, Inc.
# License:      Apache License v2.0
#----------------------------------------------------------------------------

import wx
from mainframe import MainFrame
from data import User
from config import *

class Login(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self, parent=None, id=-1, title=APP_TITLE, style=wx.DEFAULT_DIALOG_STYLE)
        sizer = wx.GridBagSizer(hgap=0, vgap=2)
        gif = wx.Image('img/logo.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap()
        logo = wx.StaticBitmap(self, -1, gif, size=(gif.GetWidth(), gif.GetHeight()))
        sizer.Add(logo, pos=(0, 0), span=(1,4), flag=wx.EXPAND)

        # Label
        sizer.Add(wx.StaticText(self, -1, u"用户名：　"), pos=(2, 1), flag=wx.ALIGN_RIGHT)
        sizer.Add(wx.StaticText(self, -1, u"密　码：　"), pos=(3, 1), flag=wx.ALIGN_RIGHT)

        # Text
        self.username = wx.TextCtrl(self, -1, size=(120, 20))
        self.username.SetMaxLength(20)
        self.password = wx.TextCtrl(self, -1, size=(120, 20), style=wx.TE_PASSWORD)
        self.password.SetMaxLength(20)
        sizer.Add(self.username, pos=(2,2), span=(1,2))
        sizer.Add(self.password, pos=(3,2), span=(1,2))

        # Button
        self.ok = wx.Button(self, -1, u"确定", (35, 20))
        self.cancel = wx.Button(self, -1, u"取消", (35, 20))
        self.Bind(wx.EVT_BUTTON, self.OnOK, self.ok)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, self.cancel)
        
        sizer.Add(self.ok, pos=(4,1))
        sizer.Add(self.cancel, pos=(4,2))
        sizer.Add((5, 0), (2, 4))
        sizer.SetEmptyCellSize((5, 30))
        self.SetSizer(sizer)
        self.Fit()
        
    def OnOK(self, event):
        user = User()
        if not self.username.GetValue():
            # wx.MessageDialog(self, u'请输入用户名。', u'提示',
                               # wx.OK | wx.ICON_INFORMATION
                               # ).ShowModal()
            wx.MessageBox(u'请输入用户名。', u"提示")
            self.username.SetFocus()
            return
            
        if not user.Check(self.username.GetValue(), self.password.GetValue()):
            # wx.MessageDialog(self, u'用户名或密码错误，请确认后重新输入。', u'错误',
                               # wx.OK | wx.ICON_ERROR
                               # ).ShowModal()
            wx.MessageBox(u'用户名或密码错误，请确认后重新输入。', u"错误")
            self.username.SetFocus()
            return
        
        self.EndModal(wx.ID_OK)

    def OnCancel(self, event):
        self.EndModal(wx.ID_CANCEL)        

class MisApp(wx.App):
    def OnInit(self):
        login = Login()
        ret = login.ShowModal()
        self.username = login.username.GetValue()
        self.password = login.password.GetValue()
        login.Destroy()

        if (ret == wx.ID_OK):
            self.frame = MainFrame()
            self.frame.Show()
            self.SetTopWindow(self.frame)
        return True

if __name__ == "__main__":
    app = MisApp()
    app.MainLoop()
